package com.example.meitao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.meitao.R;
import com.example.meitao.bean.CommunityBean;
import com.example.meitao.config.NetConfig;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by 琚雷 on 2016-9-1.
 */
public class CommunityListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<CommunityBean.DataBean.ResultsBean> mData;
    private LayoutInflater mLayoutInflater;


    public CommunityListAdapter(Context context, List<CommunityBean.DataBean.ResultsBean> data) {
        mContext = context;
        mData = data;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getEntitypicv2().size()-1;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == 0) {
            return new ViewHolder1(mLayoutInflater.inflate(R.layout.item_community_list1, parent, false));
        } else {
            return new ViewHolder2(mLayoutInflater.inflate(R.layout.item_community_list2, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder1) {
            ImageOptions options1 = new ImageOptions.Builder()
                    .setSize(DensityUtil.dip2px(60), DensityUtil.dip2px(60))
                    .setRadius(DensityUtil.dip2px(5))
                    .setCrop(true).setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    .setLoadingDrawableId(R.mipmap.ic_launcher)
                    .setFailureDrawableId(R.mipmap.ic_launcher)
                    .setUseMemCache(true).setIgnoreGif(false)
                    .setCircular(false).build();
            x.image().bind(((ViewHolder1) holder).iv_item_community_list1_userpic, NetConfig.BASE_PATH + mData.get(position).getUser().getAvatar(), options1);
            x.image().bind(((ViewHolder1) holder).iv_item_community_list1_avatar, mData.get(position).getEntitypicv2().get(0).getFilename());

            ((ViewHolder1) holder).tv_item_community_list1_nick.setText(mData.get(position).getUser().getNick());
            ((ViewHolder1) holder).tv_item_community_list1_time.setText(mData.get(position).getAgoInWords());
            ((ViewHolder1) holder).tv_item_community_list1_ename.setText(mData.get(position).getEname());
            ((ViewHolder1) holder).tv_item_community_list1_desc.setText(mData.get(position).getDesc());
            ((ViewHolder1) holder).tv_item_community_list1_collect.setText(mData.get(position).getLikes()+"");
            ((ViewHolder1) holder).tv_item_community_list1_edit.setText(mData.get(position).getComments()+"");

        } else if (holder instanceof ViewHolder2) {
            ImageOptions options2 = new ImageOptions.Builder()
                    .setSize(DensityUtil.dip2px(60), DensityUtil.dip2px(60))
                    .setRadius(DensityUtil.dip2px(5))
                    .setCrop(true).setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    .setLoadingDrawableId(R.mipmap.ic_launcher)
                    .setFailureDrawableId(R.mipmap.ic_launcher)
                    .setUseMemCache(true).setIgnoreGif(false)
                    .setCircular(false).build();
            x.image().bind(((ViewHolder2) holder).iv_item_community_list2_userpic, NetConfig.BASE_PATH + mData.get(position).getUser().getAvatar(), options2);
            x.image().bind(((ViewHolder2) holder).iv_item_community_list2_avatar1, mData.get(position).getEntitypicv2().get(0).getFilename());
            x.image().bind(((ViewHolder2) holder).iv_item_community_list2_avatar2, mData.get(position).getEntitypicv2().get(1).getFilename());

            ((ViewHolder2) holder).tv_item_community_list2_nick.setText(mData.get(position).getUser().getNick());
            ((ViewHolder2) holder).tv_item_community_list2_time.setText(mData.get(position).getAgoInWords());
            ((ViewHolder2) holder).tv_item_community_list2_ename.setText(mData.get(position).getEname());
            ((ViewHolder2) holder).tv_item_community_list2_desc.setText(mData.get(position).getDesc());
            ((ViewHolder2) holder).tv_item_community_list2_collect.setText(mData.get(position).getLikes()+"");
            ((ViewHolder2) holder).tv_item_community_list2_edit.setText(mData.get(position).getComments()+"");
        }
    }

    public static class ViewHolder1 extends RecyclerView.ViewHolder{
        private TextView tv_item_community_list1_nick, tv_item_community_list1_time, tv_item_community_list1_ename, tv_item_community_list1_desc,tv_item_community_list1_collect,tv_item_community_list1_edit;
        private ImageView iv_item_community_list1_userpic, iv_item_community_list1_avatar;
        private ListView lv_item_community_list1_reply;

        public ViewHolder1(View view) {
            super(view);
            tv_item_community_list1_nick = (TextView) view.findViewById(R.id.tv_item_community_list1_nick);
            tv_item_community_list1_time = (TextView) view.findViewById(R.id.tv_item_community_list1_time);
            tv_item_community_list1_ename = (TextView) view.findViewById(R.id.tv_item_community_list1_ename);
            tv_item_community_list1_desc = (TextView) view.findViewById(R.id.tv_item_community_list1_desc);
            tv_item_community_list1_collect = (TextView) view.findViewById(R.id.tv_item_community_list1_collect);
            tv_item_community_list1_edit = (TextView) view.findViewById(R.id.tv_item_community_list1_edit);

            iv_item_community_list1_userpic = (ImageView) view.findViewById(R.id.iv_item_community_list1_userpic);
            iv_item_community_list1_avatar = (ImageView) view.findViewById(R.id.iv_item_community_list1_avatar);

            lv_item_community_list1_reply = (ListView) view.findViewById(R.id.lv_item_community_list1_reply);
        }

    }

    public static class ViewHolder2 extends RecyclerView.ViewHolder{
        private TextView tv_item_community_list2_nick, tv_item_community_list2_time, tv_item_community_list2_ename, tv_item_community_list2_desc,tv_item_community_list2_collect,tv_item_community_list2_edit;
        private ImageView iv_item_community_list2_userpic, iv_item_community_list2_avatar1,iv_item_community_list2_avatar2;
        private ListView lv_item_community_list2_reply;

        public ViewHolder2(View view) {
            super(view);
            tv_item_community_list2_nick = (TextView) view.findViewById(R.id.tv_item_community_list2_nick);
            tv_item_community_list2_time = (TextView) view.findViewById(R.id.tv_item_community_list2_time);
            tv_item_community_list2_ename = (TextView) view.findViewById(R.id.tv_item_community_list2_ename);
            tv_item_community_list2_desc = (TextView) view.findViewById(R.id.tv_item_community_list2_desc);
            tv_item_community_list2_collect = (TextView) view.findViewById(R.id.tv_item_community_list2_collect);
            tv_item_community_list2_edit = (TextView) view.findViewById(R.id.tv_item_community_list2_edit);

            iv_item_community_list2_userpic = (ImageView) view.findViewById(R.id.iv_item_community_list2_userpic);
            iv_item_community_list2_avatar1 = (ImageView) view.findViewById(R.id.iv_item_community_list2_avatar1);
            iv_item_community_list2_avatar2 = (ImageView) view.findViewById(R.id.iv_item_community_list2_avatar2);

            lv_item_community_list2_reply = (ListView) view.findViewById(R.id.lv_item_community_list2_reply);
        }

    }
}
